package com.yt.server.common.taglib;

import java.util.List;

public class PageController<E> {

	// 总行
	private int totalRowsAmount;

	// 是否设置过totalRowsAmount
	private boolean rowsAmountSet;

	// 每页行数
	private int pageSize;

	// 当前页码
	private int currentPage = 1;

	private int nextPage;

	private int previousPage;

	// 总页
	private int totalPages;

	// 是否有下
	private boolean hasNext;

	// 是否有前
	private boolean hasPrevious;

	private int pageStartRow;

	private int pageEndRow;

	private List<E> list;

	public List<E> getList() {
		return list;
	}

	public void setList(List<E> list) {
		this.list = list;
	}

	public PageController(int totalRows) {
		setTotalRowsAmount(totalRows);
	}

	public PageController() {
	}

	/**
	 * @param i
	 * @设定总行
	 */
	public void setTotalRowsAmount(int i) {
		if (!this.rowsAmountSet) {
			totalRowsAmount = i;
			totalPages = totalRowsAmount / pageSize;
			if (totalRowsAmount % pageSize > 0) {
				totalPages++;
			}
			setCurrentPage(1);
			this.rowsAmountSet = true;
		}
	}

	/**
	 * @param i
	 * @当前页
	 */
	public void setCurrentPage(int i) {
		currentPage = i;
		nextPage = currentPage + 1;
		previousPage = currentPage - 1;
		// 计算当前页开始行和结束行
		if (currentPage * pageSize < totalRowsAmount) {
			// pageEndRow = currentPage * pageSize;
			pageStartRow = (currentPage - 1) * pageSize;

		} else {
			// pageEndRow = totalRowsAmount;
			if (currentPage == 1) {
				pageStartRow = 0;
			} else {
				pageStartRow = pageSize * (totalPages - 1);
			}
		}

		// 是否存在前页和后
		if (nextPage > totalPages) {
			hasNext = false;
		} else {
			hasNext = true;
		}
		if (previousPage == 0) {
			hasPrevious = false;
		} else {
			hasPrevious = true;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPreviousPage() {
		return previousPage;
	}

	public int getTotalPages() {
		if (totalPages > 0)
			return totalPages;
		else
			return 1;
	}

	public int getTotalRowsAmount() {
		return totalRowsAmount;
	}

	public void setHasNext(boolean b) {
		hasNext = b;
	}

	public void setHasPrevious(boolean b) {
		hasPrevious = b;
	}

	public void setNextPage(int i) {
		nextPage = i;
	}

	public void setPageSize(int i) {
		pageSize = i;
	}

	public void setPreviousPage(int i) {
		previousPage = i;
	}

	public void setTotalPages(int i) {
		totalPages = i;
	}

	public int getPageStartRow() {
		return pageStartRow;
	}

	public int getPageEndRow() {
		return pageEndRow;
	}

	public void setPageEndRow(int pageEndRow) {
		this.pageEndRow = pageEndRow;
	}
}